package com.tr.rentcar.controller.userc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserRecord;
import com.tr.rentcar.model.dao.UserDao;
import com.tr.rentcar.model.dao.UserRecordDao;


/**
 * Servlet implementation class UserRecord
 */
@WebServlet("/user/record")
public class UserRecordsController extends HttpServlet {
		UserRecordDao ur=new UserRecordDao();
		UserDao ud=new UserDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user=(User) request.getSession().getAttribute("userinfo");
		try {
			User u=new User();
			u=ud.findByid(user.getUid());
			request.setAttribute("AllCount", ur.getAllCount(user.getUid()));
			request.getSession().setAttribute("umoney", u.getUmoney());
			List<UserRecord> users = ur.findByid(user.getUid(),0);
			request.setAttribute("users", users);
			request.getRequestDispatcher("/WEB-INF/jsp/user_record.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String sum=request.getParameter("sum");
		String Current=request.getParameter("Current");
		User user=(User) request.getSession().getAttribute("userinfo");
		PrintWriter out=response.getWriter();
		if (!StringUtils.isEmpty(Current)) {
			int c=(Integer.parseInt(Current)-1)*13;
			try {
				out.println(JSONArray.toJSONString(ur.findByid(user.getUid(),c)));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return;
		}
		if (StringUtils.isEmpty(sum)) {
			doGet(request, response);
			return;
		}
		Double userRecharge=Double.valueOf(sum);
		Double umoney=Double.valueOf(request.getSession().getAttribute("umoney")+"");
		UserRecord urr=new UserRecord();
		urr.setuId(user.getUid());
		urr.setUrWay("充值");
		urr.setUrMoney(userRecharge);
		urr.setUrName(user.getUname());
		urr.setUrdoWay("用户");
		try {
			if (!ud.xgmoney(umoney+userRecharge,user.getUid())) {
				out.println("充值失败");
				return;
			}
			if (ur.save(urr)) {
				user.setUmoney(umoney+userRecharge);
				request.getSession().setAttribute("userinfo", user);
				out.println("充值成功");
				return;
			}else {
				out.println("充值失败");
				return;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
